import msg from '../../utils/msg';
import { GetActivity, AddLook, JoinActivity } from '../../utils/request/api';
import User from "../../utils/user";
var user = new User;
export interface listItem {
  title: string,
  people: string,
  place: string,
  id: number,
  grade: string,
  start_time: string,
  end_time: string,
  content: string,
  classes: string,
  isjoin: boolean,
  islook: boolean,
  count: number,
  time: string,//小程序根据起始时间整理出的时间，格式如 2023-07-29 15:00-16:00
  need: number,
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [] as listItem[],//储存数据
    //list_show:[] as listItem[],//显示的数据
    list_join: [] as listItem[],//显示的数据
    list_nojoin: [] as listItem[],//显示的数据
    user: user,
    input: '' as string,
    hasJoin:[] as number[],
    hasLook:[] as number[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {

  },
  listShow() {
    let list = this.data.list;
    let list_temp_join = [] as listItem[];
    let list_temp_nojoin = [] as listItem[];
    let hasJoin=this.data.hasJoin;
    let hasLook=this.data.hasLook;
    list.forEach((item) => {
      if(hasJoin.indexOf(item.id)!=-1) {
          console.log('item.id=',item.id);
        list_temp_join.push(item);
      }
      else list_temp_nojoin.push(item);
    })
    list_temp_nojoin.forEach((item,index)=>{
        if(hasLook.indexOf(item.id)!=-1)list_temp_nojoin[index].islook=true;
        else list_temp_nojoin[index].islook=false;
    })
    this.setData({
      list_join: list_temp_join,
      list_nojoin: list_temp_nojoin,
    });
  },
  //返回主页
  onClickLeft() {
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    //console.log('onshow');
    await this.getData();
  },
  getData: async function () {
    msg.Loading('获取活动数据中');
    return new Promise(async (resolve) => {
      await GetActivity({s_code: user.code })
        .then(async (res: any) => {
            this.setData({
              list: this.getListTime(res.data.data.showActivity),
              hasJoin:res.data.data.hasJoin,
              hasLook:res.data.data.hasLook,
            })
        });
      this.listShow();
      msg.hideLoading();
    })
  },
  //点击已报名列表跳转
  onClickList_Join(e: any) {
    const index: number = e.currentTarget.dataset.index;
    const item: listItem = this.data.list_join[index];
    wx.navigateTo({
      url: '../../pages/activity/activitydetail/activitydetail?id=' + item.id
    });
  },

  //点击未报名列表跳转
  onClickList_NoJoin(e: any) {
    const index: number = e.currentTarget.dataset.index;
    const item: listItem = this.data.list_nojoin[index];
    if (!item.islook) {
      AddLook({ a_id: item.id, s_code: user.code });
    }
    wx.navigateTo({
      url: '../../pages/activity/activitydetail/activitydetail?id=' + item.id
    });
  },


  //搜索
  getInput(e: any) {
    console.log(e.detail.value);
    let input = e.detail.value;
    let list = this.data.list;
    let temp = [] as listItem[];
    if (input == '') temp = list;
    else list.forEach((item) => {
      if (item.title.includes(input) || item.people.includes(input) || item.place.includes(input) || item.id.toString() == input) {
        temp.push(item);
      }
    });
    this.setData({
      list_show: temp,
      input: input,
    });
    this.listShow();
  },
  //整理显示的时间的格式
  getListTime(list: listItem[]) {
    for (let i = 0; i < list.length; i++) {
      let item = list[i];
      list[i].time = this._getDateParts(item.start_time) + ' ' + this._getTime(item.start_time) + '-' + this._getTime(item.end_time);
      console.log('list[i].time=',list[i].time);
    }
    return list;
  },
  formatDate(dateString: string): string {
    const parts = dateString.split(" ");
    const year = parts[0].substring(0, 4);
    const month = parts[0].substring(6, 7);
    const day = parts[0].substring(9, 10);
    const hours = parts[1].substring(0, 2);
    const minutes = parts[1].substring(4, 5);
    const seconds = parts[1].substring(7, 8);
    //console.log(`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`)
    return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
  },
  
  //获取日期的时间
  _getTime(date: string): string {
    const parsedDate = new Date(date);
    const hours = parsedDate.getHours();
    const minutes = parsedDate.getMinutes();
    return `${hours}:${minutes < 10 ? '0' + minutes : minutes}`;
  },
  //获取日期的年月日
  _getDateParts(dateString: string): string {
    const parts = dateString.split(" ");
    return parts[0];
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})